﻿@using KPIS.GERP.Models.SYS.Resources
@model KPIS.GERP.Models.ICM.PrePurchaseRequestItem

@{
    Layout = "~/Views/Shared/_EmptyLayout.cshtml";
    ViewBag.Title = "แจ้งความประสงค์ขอซื้อ/จ้าง";
    ViewBag.Descr = "ข้อมูลที่หน่วยงานต่างๆ ส่งความประสงค์ขอซื้อ/จ้างเข้ามาที่หน่วยงานพัสดุ";
}

@using (Html.BeginForm("Create", "PrePurchaseItem", FormMethod.Post, new { @class = "form-horizontal" }))
{
    @Html.AntiForgeryToken()

    <div class="block-button-menu-above">
        @Html.ActionLink("กลับไปหน้าแสดงข้อมูลทั้งหมด", "Index", new { PprId = Model.PprId }, new { @class = "btn btn-gray button-standard-width btn-small", role="button" })
        @*@Html.ButtonViewAll(false, "Index", .ToString())*@
        @Html.ButtonSubmit()
    </div>
    
    @Html.ValidationSummary(true, KPIS.GERP.Models.Class.ValidateText.RequiredField, new { @class = "alert alert-error" })
    @Html.Partial("_EditorForm")

}

@*
@section scripts
{
    <script>
        
        function ShowResource(haveItem) {
            var id = $('#ddlProjOperExpId').val();

            if (haveItem && id != '' && id != null) {
                $("div #Resource").slideDown(function () {
                    var id2 = $("#lblResItemId").val();

                    if (id2 != '' && id2 != null) {
                        $("div #Amount").slideDown();
                    }
                    else {
                        $("div #Amount").slideUp();
                    }
                });
            }
            else if (!haveItem && id != '' && id != null) {
                $("div #Resource").slideUp(function () {
                    $("div #Amount").slideDown();
                });
            }
            else {
                $("div #Resource").slideUp(function () {
                    $("div #Amount").slideUp();
                });
            }
        }

        $("form").submit(function (e) {
            e.preventDefault();
            var currentForm = this;
            bootbox.confirm(function (result) {
                if (result) {
                    currentForm.submit();
                }
            });
        });

        $(function () {
            ShowResource();

            $("#ddlProjOperId").change(function () {
                $('#ddlResItemId').val('');

                var id = $('#ddlProjOperId').val();
                $('#ddlProjOperExpId').empty();
                if (id != '') {
                    $.getJSON('@Url.Action("GetProjectOperationExpense")/' + id, function (data) {

                        var items = '';
                        $.each(data, function (i, result) {
                            $('#ddlProjOperExpId').append("<option value='" + result.Value + "'>" + result.Text + "</option>");
                        });
                    });
                }
                else {
                    $("div #Resource").hide("slow");
                    $("div #Amount").hide("slow");
                }
            });

            $('#ddlProjOperExpId').change(function () {
                $('#lblResItemId').val('');

                var id = $('#ddlProjOperExpId').val();
                if (id == "") {
                    $("div #Resource").hide("slow");
                    $("div #Amount").hide("slow");
                }
                else {
                    var lblResItemId = $('#lblResItemId');
                    var lblResItemName = $('#lblResItemName');
                    var lblResSubGrpName = $('#lblResSubGrpName');
                    var lblResGrpName = $('#lblResGrpName');
                    var lblResTypeName = $('#lblResTypeName');
                    var ddlResGrpId = $('#ddlResGrpId_chzn');
                    var ddlResSubGrpId = $('#ddlResSubGrpId_chzn');
                    var ddlResItemId = $('#ddlResItemId');

                    $.getJSON('@Url.Action("GetResourceTypeInfo")?ProjOperExpId=' + id, function (data) {
                        if (data.ProjectExpense != undefined) {
                            lblResItemId.val(data.ProjectExpense.ResItemId);
                            lblResItemName.val(data.ProjectExpense.MasResourceItem.Name);

                            if (data.ProjectExpense == undefined) alert('ProjectExpense is undefined');
                            if (data.ProjectExpense.MasResourceItem == undefined) alert('MasResourceItem is undefined');
                            if (data.ProjectExpense.MasResourceItem.MasResourceSubGroup == undefined) alert('MasResourceSubGroup is undefined');
                            if (data.ProjectExpense.MasResourceItem.MasResourceSubGroup.MasResourceGroup.MasResourceType == undefined) alert('MasResourceType is undefined');
                            lblResSubGrpName.val(data.ProjectExpense.MasResourceItem.MasResourceSubGroup.Name);
                            lblResGrpName.val(data.ProjectExpense.MasResourceItem.MasResourceSubGroup.MasResourceGroup.Name);
                            lblResTypeName.val(data.ProjectExpense.MasResourceItem.MasResourceSubGroup.MasResourceGroup.MasResourceType.Name);

                            $("#lblDescr").val(data.Descr);
                            $("#lblUnitName").val(data.ProjectExpense.MasResourceItem.MasUnit.Name);
                            $("#lblStdPrice").val(addCommas(data.ProjectExpense.MasResourceItem.StdPrice));
                            $("#lblPrice").val(addCommas(data.UnitPrice));
                            $("#lblQty").val(data.UnitQty);
                            $("#lblAmount").val(addCommas(data.Amount));

                            $('#lblResItemName').show();
                            $('#lblResSubGrpName').show();
                            $('#lblResGrpName').show();
                            $('#ddlResGrpId_chzn').hide();
                            $('#ddlResSubGrpId_chzn').hide();
                            $('#ddlResItemId').hide();

                            ShowResource($('input[name=HaveItem]:checked').val());
                            $("div #Amount").show("slow");
                        }
                        else {
                            lblResTypeName.val(data.Name);
                            $('#ddlResSubGrpId').empty();
                            $('#ddlResItemId').empty();
                            $("#ddlResSubGrpId").trigger("liszt:updated");//update list

                            $.getJSON('@Url.Action("GetResourceGroupList")?ResTypeId=1', function (data) {// hardcode
                                $('#ddlResGrpId').empty();
                                var items = '';
                                $.each(data, function (i, result) {
                                    $('#ddlResGrpId').append("<option value='" + result.Value + "'>" + result.Text + "</option>");
                                });

                                $("#ddlResGrpId").trigger("liszt:updated");//update list
                            });

                            $("#lblDescr").val('');
                            $("#lblUnitName").val('');
                            $("#lblStdPrice").val(addCommas('0'));
                            $("#lblPrice").val(addCommas('0'));
                            $("#lblQty").val('0');
                            $("#lblAmount").val(addCommas('0'));

                            lblResItemName.hide();
                            lblResSubGrpName.hide();
                            lblResGrpName.hide();

                            ddlResGrpId.show();
                            ddlResSubGrpId.show();
                            ddlResItemId.show();

                            ShowResource($('input[name=HaveItem]:checked').val());
                        }
                    });
                }
            });

            $("#ddlResGrpId").change(function () {
                var id = $('#ddlResGrpId').val();

                if (id != '') {
                    $.getJSON('@Url.Action("GetResourceSubGroupList")?ResGrpId=' + id, function (data) {
                        $('#ddlResSubGrpId').empty();
                        $('#ddlResItemId').empty();
                        var items = '';
                        $.each(data, function (i, result) {
                            $('#ddlResSubGrpId').append("<option value='" + result.Value + "'>" + result.Text + "</option>");
                        });
                        $("#ddlResSubGrpId").trigger("liszt:updated");//update list
                    });
                }
                else {
                    $('#ddlResItemId').empty();
                    $("div #Amount").hide("slow");
                }
            });

            $("#ddlResSubGrpId").change(function () {
                var id = $('#ddlResSubGrpId').val();

                if (id != '') {
                    $.getJSON('@Url.Action("GetResourceItemList")?ResSubGrpId=' + id, function (data) {
                        $('#ddlResItemId').empty();
                        var items = '';
                        $.each(data, function (i, result) {
                            $('#ddlResItemId').append("<option value='" + result.Value + "'>" + result.Text + "</option>");
                        });
                    });
                }
                else {
                    $("div #Amount").hide("slow");
                }
            });

            $("#ddlResItemId").change(function () {
                var id = $('#ddlResItemId').val();

                if (id != '') {
                    $("#lblResItemId").val(id);
                    $.getJSON('@Url.Action("GetResourceItemInfo")/' + id, function (data) {
                        $("#lblUnitName").val(data.MasUnit != null ? data.MasUnit.Name : "หน่วย");
                        $("#lblStdPrice").val(addCommas(data.StdPrice));
                        $("#lblPrice").val(addCommas(data.StdPrice));
                        $("div #Amount").show("slow");
                    });
                }
                else {
                    $("div #Amount").hide("slow");
                }
            });

            $("#lblPrice").change(function () {
                var price = $("#lblPrice").val();
                var qty = $("#lblQty").val();
                var lblAmount = $("#lblAmount");

                if (price == "") {
                    price = 0;
                }

                if (qty == "") {
                    qty = 0;
                }

                lblAmount.val(addCommas(price * qty));
            });

            $("#lblQty").change(function () {
                var price = $("#lblPrice").val();
                var qty = $("#lblQty").val();
                var lblAmount = $("#lblAmount");

                if (price == "") {
                    price = 0;
                }

                if (qty == "") {
                    qty = 0;
                }

                lblAmount.val(addCommas(price * qty));
                $("#lblQty").val(addCommasOnly(qty));
            });
        });
    </script>
}*@